package net.xdclass.controller;


import lombok.extern.slf4j.Slf4j;
import net.xdclass.annotation.RepeatSubmit;
import net.xdclass.constant.RedisKey;
import net.xdclass.controller.request.ConfirmOrderRequest;
import net.xdclass.controller.request.ShopPageRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.ClientTypeEnum;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.model.LoginUser;
import net.xdclass.model.ProductOrderDO;
import net.xdclass.service.ProductOrderService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小冯
 * @since 2023-07-18
 */
@RestController
@RequestMapping("/api/order/v1")
@Slf4j
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取token
     * @return
     */
    @GetMapping("token")
    public JsonData getToken(){
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        String token = CommonUtil.getStringNumRandom(32);
        //"order:submit:%s:%s"
        String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY, loginUser.getAccountNo(),token);

        redisTemplate.opsForValue().set(key, "1", 30, TimeUnit.MINUTES);

        return JsonData.buildSuccess(token);


    }
    /**
     * 分页查询
     * @return
     */
    @PostMapping("page")
    @RepeatSubmit(limitType = RepeatSubmit.Type.TOKEN)
    public JsonData page(@RequestBody ShopPageRequest shopPageRequest){
        Map<String, Object> pageResult = productOrderService.page(shopPageRequest.getPage(), shopPageRequest.getSize(), shopPageRequest.getState());
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 查询订单状态
     * @param outTradeNo
     * @return
     */
    @GetMapping("queryState")
    public JsonData queryState( @RequestParam(value = "out_trade_no") String outTradeNo){
       String state = productOrderService.queryProductState(outTradeNo);
        return StringUtils.isNotBlank(state)?JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST):JsonData.buildSuccess(state);
    }


    /**
     * 查询订单状态
     * @param
     * @return
     */
    @PostMapping("confirm")
    public JsonData confirm(@RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response){
        JsonData jsonData = productOrderService.confirmOrder(orderRequest);
        if (jsonData.getCode() == 0) {
            //端类型
            String clientType = orderRequest.getClientType();
            //支付类型
            String payType = orderRequest.getPayType();
            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALI_PAY.name())) {
                //如果是支付宝  跳转页面 返回的是html sdk除外
                if (clientType.equalsIgnoreCase(ClientTypeEnum.PC.name())) {
                    CommonUtil.sendHtmlMessage(response,jsonData);
                } else if (clientType.equalsIgnoreCase(ClientTypeEnum.APP.name())) {

                } else if (clientType.equalsIgnoreCase(ClientTypeEnum.H5.name())) {

                }
            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT_APY.name())) {
                //如果是微信支付 返回json
                CommonUtil.sendJsonMessage(response,jsonData);

            }


        }else {
            log.error("创建订单失败{}",jsonData.getData().toString());
            CommonUtil.sendJsonMessage(response,jsonData);
        }
        return jsonData;
    }

}

